package erz.printing.police.adapter;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import me.iwf.photopicker.PhotoPicker;

import java.io.File;
import java.util.List;

import erz.printing.police.R;

public class TakePhotoAdapter extends BaseAdapter implements View.OnClickListener {
    private  class ItemTag {
        public String photoPath;
        public Button btnDel;
        public TextView tvTip;
        public ImageView imageView;
    }

    private Context mContext;
    private int parentWidth;
    private List<String> items;

    public TakePhotoAdapter(List<String> items, Context context, int parentWidth) {
        this.items = items;
        this.mContext = context;
        this.parentWidth = parentWidth;
    }

    public void setData(List<String> items) {
        this.items = items;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        parentWidth = ((Activity)mContext).findViewById(R.id.images_gridView).getWidth();
        if (parentWidth == 0) return 0;
        return items.size() + 1;
    }

    @Override
    public String getItem(int position) {
        if (position < items.size()) {
            return items.get(position);
        }
        return "add";
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        ItemTag tag;
        parentWidth = ((Activity)mContext).findViewById(R.id.images_gridView).getWidth();
        int size = parentWidth / 2;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.photo_item, null);
            tag = new ItemTag();
            tag.btnDel = convertView.findViewById(R.id.photo_del);
            tag.tvTip = convertView.findViewById(R.id.photo_add_tip);
            tag.imageView = convertView.findViewById(R.id.photo);
            convertView.setTag(tag);
            imageView = tag.imageView;

            AbsListView.LayoutParams params = new AbsListView.LayoutParams(size, size);
            convertView.setLayoutParams(params);
        } else {
            tag = (ItemTag) convertView.getTag();
            imageView = tag.imageView;
        }

        if (position == items.size()) {
            tag.btnDel.setVisibility(View.GONE);
            tag.tvTip.setVisibility(View.VISIBLE);
            imageView.setClickable(true);
            RelativeLayout.LayoutParams rlParams = new RelativeLayout.LayoutParams(size/2, size/2);
            rlParams.addRule(RelativeLayout.CENTER_IN_PARENT);
            imageView.setLayoutParams(rlParams);
            imageView.setOnClickListener(this);
            //imageView.setImageResource(R.mipmap.add_image);
            Glide.with(mContext).load(R.mipmap.add_image).into(imageView);
        } else {
            tag.btnDel.setVisibility(View.VISIBLE);
            tag.tvTip.setVisibility(View.GONE);
            tag.photoPath = getItem(position);
            tag.btnDel.setTag(tag);
            tag.btnDel.setOnClickListener(this);
            imageView.setClickable(false);
            RelativeLayout.LayoutParams rlParams = new RelativeLayout.LayoutParams(-1, -1);//match_parent
            rlParams.addRule(RelativeLayout.CENTER_IN_PARENT);
            imageView.setLayoutParams(rlParams);
            Glide.with(mContext).load(Uri.fromFile(new File(getItem(position)))).into(imageView);
        }

        return convertView;
    }

    @Override
    public void onClick(View view) {
        if (view instanceof ImageView) {
            PhotoPicker.builder()
                    .setPhotoCount(9)
                    .setShowCamera(true)
                    .setPreviewEnabled(false)
                    .start((Activity) mContext, PhotoPicker.REQUEST_CODE);
        } else {
            ItemTag tag = (ItemTag) view.getTag();
            delPhoto(tag.photoPath);
        }
    }

    private void delPhoto(String photo) {
        items.remove(photo);
        notifyDataSetChanged();
    }

    public void addPhotos(List<String> photos) {
        boolean bChanged = false;
        for (String tmp : photos) {
            boolean found = false;
            for (String item : items) {
                if (tmp.equals(item)) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                items.add(tmp);
                bChanged = true;
            }
        }
        if (bChanged) {
            notifyDataSetChanged();
        }
    }

    public List<String> getPhotos() {
        return items;
    }
}
